package com.example.allplan.ShuJuKuCaoZuo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import com.example.allplan.JavaBean.BiaoQingLieBiao;
import com.example.allplan.JavaBean.JiHuaBaoJavaBean;
import com.example.allplan.RecyclerAdapter.JiHuaBaoRecyclerAdapter;
import com.example.allplan.SQhelper.SuoYouJiHuaBaoSQhelper;

import java.util.ArrayList;

public class SuoYouJiHuaBaoSQCaoZuo {
    public final String JI_HUA_BAO_SHU_JU_KU_WEN_JIAN_MING="ji_hua_bao_shu_ju_ku_wen_jian";
    public SuoYouJiHuaBaoSQhelper mdbhelp;
    public SQLiteDatabase mDatabase;
    public Context mContext;
    public static ContentValues bean_to_values(JiHuaBaoJavaBean o){
        ContentValues values=new ContentValues();
        values.put("id",o.getId());
        values.put("pai_xu_you_biao",o.getPai_xu_you_biao());
        values.put("biao_qing_fu_hao_zhi",o.getBiao_qing_fu_hao_zhi());
        values.put("lei_xing",o.getLei_xing());
        values.put("ji_hua_shu",o.getJi_hua_shu());
        values.put("bao_ming",o.getBao_ming());
        return values;
    }
    public static ContentValues bean_to_value_only_biao_qing_and_bao_ming(JiHuaBaoJavaBean o){
        ContentValues values=new ContentValues();
        values.put("id",o.getId());
        values.put("biao_qing_fu_hao_zhi",o.getBiao_qing_fu_hao_zhi());
        values.put("bao_ming",o.getBao_ming());
        return values;
    }
    public SuoYouJiHuaBaoSQCaoZuo(Context mC) {
        mContext=mC;
        mdbhelp=new SuoYouJiHuaBaoSQhelper(mContext,JI_HUA_BAO_SHU_JU_KU_WEN_JIAN_MING,null,1);
    }
    public synchronized void openRead(){
        mDatabase=mdbhelp.getReadableDatabase();
    }
    public synchronized void close(){
        mDatabase.close();
    }
    public synchronized void openWrite(){
        mDatabase=mdbhelp.getWritableDatabase();
    }
    public synchronized ArrayList<JiHuaBaoJavaBean> reloadAllJiHuaBao(){
        ArrayList<JiHuaBaoJavaBean> mList=new ArrayList<JiHuaBaoJavaBean>();
        openWrite();
        Cursor cursor = mDatabase.query("ji_hua_bao_biao", new String[]{"id","pai_xu_you_biao","biao_qing_fu_hao_zhi","lei_xing","ji_hua_shu","bao_ming"}, null, null, null, null, "id");
        while (cursor.moveToNext()) {
           mList.add(new JiHuaBaoJavaBean(cursor.getInt(cursor.getColumnIndex("id")),cursor.getInt(cursor.getColumnIndex("pai_xu_you_biao")),cursor.getInt(cursor.getColumnIndex("biao_qing_fu_hao_zhi")),cursor.getInt(cursor.getColumnIndex("lei_xing")),cursor.getInt(cursor.getColumnIndex("ji_hua_shu")),cursor.getString(cursor.getColumnIndex("bao_ming"))));
        }
        close();
        return mList;
    }
    public synchronized JiHuaBaoJavaBean reloadByIdJiHuaBao(int n){
        JiHuaBaoJavaBean mo=new JiHuaBaoJavaBean();
        openWrite();
        Cursor cursor = mDatabase.query("ji_hua_bao_biao", new String[]{"id","pai_xu_you_biao","biao_qing_fu_hao_zhi","lei_xing","ji_hua_shu","bao_ming"}, "id=?", new String[]{n+""}, null, null, "id");
      cursor.moveToNext();
           mo =new JiHuaBaoJavaBean(cursor.getInt(cursor.getColumnIndex("id")),cursor.getInt(cursor.getColumnIndex("pai_xu_you_biao")),cursor.getInt(cursor.getColumnIndex("biao_qing_fu_hao_zhi")),cursor.getInt(cursor.getColumnIndex("lei_xing")),cursor.getInt(cursor.getColumnIndex("ji_hua_shu")),cursor.getString(cursor.getColumnIndex("bao_ming")));

        close();
        return mo;
    }
    public synchronized void write_one(JiHuaBaoJavaBean o){
        openWrite();
        mDatabase.insert("ji_hua_bao_biao",null,bean_to_values(o));
        close();
    }
    public synchronized void update_one(JiHuaBaoJavaBean o){
        openWrite();
        mDatabase.update("ji_hua_bao_biao",bean_to_values(o),"id=?",new String[]{o.getId()+""});
        close();
    }
    public synchronized void update_one_only_biao_qing_and_bao_ming(JiHuaBaoJavaBean o){
        openWrite();
        mDatabase.update("ji_hua_bao_biao",bean_to_value_only_biao_qing_and_bao_ming(o),"id=?",new String[]{o.getId()+""});
        close();
    }
    public  synchronized void delete_suo_you_chuang_jian(){
        openWrite();
        mDatabase.delete("ji_hua_bao_biao","id>?",new String[]{"1"});
        close();
    }
}
